package com.zjc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjc.common.R;
import com.zjc.pojo.StuRoom;
import com.zjc.pojo.Student;
import com.zjc.service.BuliderService;
import com.zjc.service.StuRoomService;
import com.zjc.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周佳成
 * @since 2023-09-17
 */
@RestController
@RequestMapping("/api/student")
public class StudentController {
		@Autowired
	    StudentService studentService;

		@Autowired
	    StuRoomService stuRoomService;

		@Autowired
		BuliderService buliderService;

	@GetMapping("/find")
	public R<Page> GetStudentPage(int pageNum, int pageSize, String search){
		QueryWrapper<Student> wrapper = new QueryWrapper<>();

		wrapper.like("stu_name",search);
		wrapper.orderByAsc("stu_id");
		Page<Student> page1 = new Page<>(pageNum,pageSize);
		studentService.page(page1,wrapper);
		return R.success(page1);
	}
	@PostMapping("/login")
	public R<Student> login(HttpServletRequest httpServletRequest, @RequestBody Student student){
		String password = student.getStuPassword();
		LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();

		lambdaQueryWrapper.eq(Student::getStuId,student.getStuId());

		Student stu = studentService.getOne(lambdaQueryWrapper);

		if(stu == null){
			return R.error("用户名不存在!");
		}
		else{
			String pw = stu.getStuPassword();
			if(!pw.equals(password)){
				return R.error("密码错误!");
			}
			else{
					httpServletRequest.getSession().setAttribute("user",stu.getStuId());

					return R.success(stu);
			}
		}
	}
	@PutMapping("/update")
	public R<String> changeStu(@RequestBody Student student){
		Integer roomid = student.getRoomId();

		if(!studentService.isRoomExists(roomid)){
			return R.error("房间号不存在！");
		}
		boolean rows=studentService.updateById(student);
		System.out.println(rows);

		return R.success("修改成功");
	}

	@PostMapping("/add")
	public R<String> addStu(@RequestBody Student student){
		Integer stuid = student.getStuId();
		Integer ldid = student.getLdId();
		int sextypde = student.getStuSex().equals("男")? 1:0;
		if(studentService.isStuExists(stuid)){
			return R.error("学号已经存在！！请勿重复添加");
		}
		Integer roomid = student.getRoomId();
		if(!studentService.isRoomExists(roomid)){
			return R.error("房间号不存在！");
		}
		if(!buliderService.isExistBulider(ldid)){
			return R.error("楼栋不存在！！");
		}
		if(sextypde!= buliderService.BuliderType(ldid)){
			return R.error("请勿搞混男女宿舍！！");
		}
		studentService.save(student);
		return R.success("添加成功");
	}

	@DeleteMapping("/delete/{stuId}")
	public R<String> deleteStudent(@PathVariable Integer stuId){
		System.out.println(stuId);
		int rows=studentService.deleteStu(stuId);
		if(rows!=0){
			return R.success("删除成功");
		}
		else{
			return R.error("删除失败");
		}
	}
//获取男女生人数
	@GetMapping("/stuCount")
	public R<StuCount> getBoyandGirl(){
		LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(Student::getStuSex,"男");
		Long numBoy = studentService.count(lambdaQueryWrapper);
		LambdaQueryWrapper<Student> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
		lambdaQueryWrapper1.eq(Student::getStuSex,"女");
		Long numGirl = studentService.count(lambdaQueryWrapper1);
		return R.success(new StuCount(numGirl,numBoy));
	}
	//获取室友信息
	@GetMapping("/info")
	public R<List<Student>> getRoomMate(int ldId,int roomId,int stuId){
		LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(Student::getLdId,ldId)
				.eq(Student::getRoomId,roomId).ne(Student::getStuId,stuId);
		List<Student> lists = new ArrayList<>();
		lists = studentService.list(lambdaQueryWrapper);

		return R.success(lists);


	}



}

